var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    receive_time: '00:00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if (e.id) {
      this.setData({
        id: e.id
      })
    }
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  loadData: function () {
    var that = this;
  },
  backToList: function () {
    wx.redirectTo({
      url: '/pages/enterprise/shop'
    })
  },
  setProject: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse?user=shop&warehouse=' + that.data.warehouse
    })
  },
  selectCustomer: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/customerList'
    })
  },
  formSubmit: function () {
    var that = this;
    var dataList = that.data.dataList;
    if(!that.data.coord){
      wx.showToast({
        title: "请选择坐标",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    if(!that.data.name){
      wx.showToast({
        title: "请输入名称",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("/atapi/shop/shop/newShop", {
      'CO_ID': app.globalData.CO_ID,
      'name': that.data.name,
      'address': that.data.address,
      'coord': that.data.coord,
      'receive_time': that.data.receive_time,
      'manager': that.data.manager,
      'tel': that.data.tel,
      'warehouse': that.data.warehouse||'',
      'customer': that.data.customer||'',
      'province': that.data.addressComponent.province,
      'city': that.data.addressComponent.city,
      'area': that.data.addressComponent.area
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        wx.showToast({
          title: "提交成功",
          icon: "success",
          duration: 3000,
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/enterprise/shop'
              })
            }, 3000);
          }
        });
      } else {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  showLocation:function(){
    var that = this;
    var coord = that.data.coord
    if (coord) {
      coord = coord.split(',');
      wx.openLocation({
        latitude: Number(coord[0]),
        longitude: Number(coord[1]),
        scale: 18
      })
    } else {
      wx.showToast({
        title: "请先选择位置",
        image: '/images/wrong.png',
        duration: 3000
      });
    }
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success(res) {
        console.log(res)
        var latitude = res.latitude;
        var longitude = res.longitude;
        that.setData({
          coord: latitude + ',' + longitude
        })
        if (!that.data.address){
          that.setData({
            address: res.address
          })
        }
        if (!that.data.name) {
          that.setData({
            name: res.name
          })
        }
        util.getData("/xypapi/map/routerCalculate/geocode", {
          latitude: res.latitude,
          longitude: res.longitude
        }, function (res, error) {
          console.log(res); console.log(error)
          if (res.data.success) {
            that.setData({
              "addressComponent.province": res.data.component.province,
              "addressComponent.city": res.data.component.city,
              "addressComponent.area": res.data.component.district
            });
          }
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'path_customer',
      success: function (res) {
        console.log(res)
        if (res.data) {
          that.setData({
            customerName: res.data.name,
            customer:res.data.value
          });
          wx.removeStorage({
            key: 'path_customer',
            success: function (res) {
            }
          });
        }
      }
    });
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          var data = res.data;
          var warehouse = [], wh_name = [];
          for (var i = 0; i < data.length; i++) {
            warehouse.push(data[i].value);
            wh_name.push(data[i].name);
          }
          that.setData({
            warehouse: warehouse.join(','),
            wh_name: wh_name.join(',')
          });
          wx.removeStorage({
            key: 'monitor_warehouse',
            success: function (res) {
            }
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})